简体   繁体   中英

Shiny save as pdf or plot

Here is my code I have 3 different questions, I searched evrywhere and tryied sooo many things, but it always gave me an error . First how can I add a function to save my rglwidgetoutput to any file ? And also mby save the log . Secound: If I open the program it always opens a small "focus" window, can I somehow remove that ? And last but not least, I have a Log , and I want to rename the data_planes so the logfile looks better :)

#######################################################################################
#                                 Install librarys                                    #
#######################################################################################
#install.packages("shiny")
#install.packages("rgl")
#install.packages("shinythemes")
#install.packages("devtools")

library(shiny)
library(rgl)
library(shinythemes)
library(devtools)
#install_github("rgl", "trestletech", "js-class")
#install_github("rgl", "trestletech", "js-class")

#######################################################################################
#                                   User Interface                                    #
#######################################################################################

ui <- fluidPage(theme = shinytheme("slate"),

  headerPanel("Block Theory"),

  sidebarPanel(

     numericInput(inputId = "dd", label = "Dip direction:", value = "", width = "80%", min = 0, max = 360),

     numericInput(inputId = "fa", label = "Fracture angle:", value = "", width = "80%", min = 0, max = 90),

     numericInput(inputId = "position_x", label = "Position:", value = "", width = "40%"),

     numericInput(inputId = "position_y", label = "", value = "", width = "40%"),

     numericInput(inputId = "position_z", label = "", value = "", width = "40%"),

     #selectInput("form", "Form:",
     #            c("Circle", "Square", "Ellipsoid")),

     actionButton(inputId = "add", label = "Add a plane"),

     actionButton(inputId = "plotbutton", label = "Update")

  ),

  mainPanel(  
    tabsetPanel(

      tabPanel("Plot", rglwidgetOutput(outputId = "plot")), # Output

      tabPanel("Log", verbatimTextOutput(outputId = "log_planes")), # Log File

      # OPTIONS : 

      tabPanel("Preferences", 

               checkboxInput("axes_lim", "axes min / max"),
               conditionalPanel(
                 condition = "input.axes_lim == true",
                 splitLayout(
                 numericInput(inputId = "min_x", label = "x min:", value = "0", width = "90%"),
                 numericInput(inputId = "max_x", label = "x max:", value = "1000", width = "90%")),
                 splitLayout(
                 numericInput(inputId = "min_y", label = "y min:", value = "0", width = "90%"),
                 numericInput(inputId = "max_y", label = "y max:", value = "1000", width = "90%")),
                 splitLayout(
                 numericInput(inputId = "min_z", label = "z min:", value = "0", width = "90%"),
                 numericInput(inputId = "max_z", label = "z max:", value = "1000", width = "90%"))),

               checkboxInput("axes", "Change axes ratio"),
               conditionalPanel(
                 condition = "input.axes == true",

                 sliderInput("x_axis", "x axis:",min = 0, max = 1, value = 1, step = 0.1),

                 sliderInput("y_axis", "y axis:",min = 0, max = 1, value = 1, step = 0.1),     

                 sliderInput("z_axis", "z axis:",min = 0, max = 1, value = 1, step = 0.1)),

               checkboxInput("theme", "Change shiny theme"),
               conditionalPanel(
                condition = "input.theme == true",
                  shinythemes::themeSelector()  )
              ))

  ) # /Main panel
) # /ui


#######################################################################################
#                                       SERVER                                        #
#######################################################################################


server <- function(input, output) {

  data_planes <- data.frame()
  makeReactiveBinding("data_planes")  



  observe({
    input$add
    isolate({
      data_planes <<- rbind(data_planes, data.frame(input$dd, input$fa , input$position_x , input$position_y , input$position_z))
      data_planes <<- na.omit(data_planes)

    })
  })  


  output$plot <- renderRglwidget({

    input$plotbutton

isolate({

  ####################################################     
  #    Open 3d plot:
  x<-sample(input$min_x:input$max_x, 100)
  y<-sample(input$min_y:input$max_y, 100)
  z<-sample(input$min_z:input$max_z, 100)
  plot3d(x, y, z, type = "n",xlim = c(min(x), max(x)), ylim = c(min(y), max(y)), zlim = c(min(z), max(z),expand = 1.03))
  aspect3d(input$x_axis , input$y_axis , input$z_axis)
  ####################################################      

    i=1;
    while (i <= nrow(data_planes)) {

      phi <- data_planes[i,1] * pi / 180
      theta <- data_planes[i,2] * pi / 180
      Px <- data_planes[i,3]
      Py <- data_planes[i,4]
      Pz <- data_planes[i,5]
      n <- c(sin(theta)*sin(phi), sin(theta) * cos(phi), cos(theta))
      # n <- c(-sin(theta)*sin(phi), sin(theta) * cos(phi), -cos(theta))

      P_n <- cos(phi)*sin(theta)*Px+(sin(phi)*sin(theta))*Py+cos(phi)*Pz   # d = -P * n

      # planes3d() plots equation:   a*x + b*y +  c*z + d = 0         

      a <- -sin(theta)*sin(phi)
      b <- sin(theta) * cos(phi)
      c <- -cos(theta)
      d <- P_n          


      cols<-rgb(runif(5),runif(5),runif(5))  #random color genarator

      i <- i + 1

      planes3d(a, b, c , d , col = cols, alpha = 0.6) 

    }

    rglwidget() # opens the plot inside of main panel

  })
  })


  output$log_planes <- renderPrint(data_planes)


}
#######################################################################################
shinyApp(ui = ui, server = server

)

It's not easy to save rgl output to a PDF. You can save it to an html page using code like this:

htmlwidgets::saveWidget(rglwidget(), file = "rgl.html")

This will fail if it can't find Pandoc; you can use

htmlwidgets::saveWidget(rglwidget(), file = "rgl.html", selfcontained = FALSE)

without Pandoc, but it will create both the HTML file and a subdir of supporting files.

The little window you're seeing is probably the rgl output window. If you never want to see that, run

options(rgl.useNULL = TRUE)

before loading the rgl package. This is a good idea on a Shiny app, because they may be running on a server somewhere and you don't want to try to open an rgl window there.

Sorry, I don't really understand your third question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM