2013-04-08 57 views
4

我在Shiny Google小组上提出过这个问题,但一旦发布,它立即被删除,我不知道为什么。如何保存(不上传)从Shiny界面创建的文件?

所以我在这里问这个问题。

我知道如何上传从Shiny应用程序创建的文件,但我没有成功花费几个小时来找到如何将文件保存在硬盘上。请你能告诉我一个办法吗?例如,我想保存使用sink()或RData文件创建的文件。

下面是我多次尝试之一的一个(人造)例子。 sweaveSave()函数不起作用。请不要关注剧情,它不会影响我的问题。

server.R

library(shiny) 
## 
## function creating a Sweave report 
## 
createReport <- function(file){ 
     sink(file) 
     cat(
"\\documentclass{article}\n 
\\begin{document}\n 
\\SweaveOpts{concordance=TRUE} 
This is the Rnw file.\n 
<<fig=TRUE>>= 
plot(0,0) 
@\n 
\\end{document}\n") 
     sink() 
} 

## 
## Shiny server 
## 
shinyServer(function(input, output) { 
    ## 
    ## Create plot 
    ## 
    createPlot <- reactive({ 
     # generate an rnorm distribution and plot it 
     titl <- paste0("Exponential distribution with rate ", round(input$parameter,2)) 
    curve(dexp(x,rate=input$parameter), from=0, to=5, main=titl, ylab=NA, xlab=NA) 
     }) 
    ## 
    ## output : plot 
    ## 
    output$distPlot <- renderPlot({ 
     createPlot() 
    }) 
    ## 
    ## output : download Sweave file 
    ## 
    output$sweavedownload <- downloadHandler(
     filename="report00.Rnw", 
     content = createReport 
    ) 
    ## 
    ## save Sweave file 
    ## 
    sweaveSave <- reactive({ 
     if(input$save){ 
       createReport("REPORT00.Rnw") 
     }else{NULL} 
    }) 
}) 

ui.R

library(shiny) 
shinyUI(pageWithSidebar(

    # Application title 
    headerPanel("Hello Shiny!"), 

    # Sidebar panel 
    sidebarPanel(
    sliderInput("parameter", 
       "Rate parameter:", 
       min = 0.0000000001, 
       max = 10, 
       value = 5), 
    checkboxInput("save", "Check to save and download") 
), 

    # Main panel 
    mainPanel(
    plotOutput("distPlot"), 
    conditionalPanel(
     condition = "input.save", 
     downloadLink("sweavedownload", "Download") 
    ) 
) 
)) 
+0

它似乎适用于我:选中该复选框会使下载链接出现,然后单击下载链接创建并下载.Rnw文件。 'sweaveSave'无效表达式不执行,因为没有任何东西调用它;反应式表达式被懒惰地评估。如果你想让它执行即使没有任何调用它,你需要改为观察者,或者用观察者调用它。请参阅http://rstudio.github.io/shiny/tutorial/#reactivity-overview以获取有关被动表达式和观察者之间区别的更多信息。 – 2013-04-08 22:42:32

+0

@JoeCheng谢谢Joe。我的邮件今天在Shiny Google群组上仍然是垃圾邮件,我无法回复! 你说这对你有用。但创建的文件在哪里?对我而言,它不会出现在硬盘的任何地方。 无论如何,我也想创建一个文件,而无需下载它。 – 2013-04-09 07:30:44

+0

@JoeCheng谢谢!它与'observe()'函数一起工作! – 2013-04-09 08:44:15

回答

1

它使您的生活使用shinyFiles包容易。

install.package('shinyFiles') 
require(shinyFiles) 
shinyFilesExample()