【问题标题】:How to access data stored in server.R from outside the server/UI如何从服务器/用户界面外部访问存储在 server.R 中的数据
【发布时间】:2019-10-25 21:51:37
【问题描述】:

我对 R/Rshiny/RMarkdown 很陌生,我已经使用了一些基本的用户输入模板来构建应用程序。我想要的是用户输入文件路径并能够在另一个函数/块中调用文件名和文件路径。

我发现响应式可能是执行此操作的关键功能,但我不确定如何使用它。

library(shinyFiles)


  ui <- fluidPage(
    shinyFilesButton("Btn_GetFile", "Choose a file" ,
                                    title = "Please select a file:", multiple = TRUE,
                          buttonType = "default", class = NULL),

              textOutput("filename"), 
              textOutput("txt_file")
                   )

  filepath <- reactiveValues() #trying, but this isn't quite working
  server <- function(input,output,session){

    volumes = getVolumes()
    observe({  
    shinyFileChoose(input, "Btn_GetFile", roots = volumes, session = session)

    if(!is.null(input$Btn_GetFile)){
      # browser()
      file_selected<-parseFilePaths(volumes, input$Btn_GetFile)
      output$txt_file <- renderText(as.character(file_selected$datapath))
      output$filename <- renderText(as.character(file_selected$name))
      filepath <- reactive({output$txt_file}) #an attempt, but it isn't doing what I want.
    }
  })
  }

  shinyApp(ui = ui, server = server)
  isolate(filepath) #also tried print. Really just trying to see if filepath is populated

如果用户输入 /Users/Jim/tmp.txt,我希望看到 UI 显示 /Users/Jim/tmp.txt 和 tmp.txt(它确实如此)并且还看到 /Users/Jim/ tmp.txt 被存储为我可以在其他地方访问的文件路径变量。

【问题讨论】:

    标签: r shiny markdown r-markdown


    【解决方案1】:

    看看?reactiveValues。修改了您的代码,这应该可以工作: 编辑根据您的评论,您可以尝试将system() 命令与glue 包结合使用

    library(shinyFiles)
    library(glue)
    
    ui <- fluidPage(
      shinyFilesButton("Btn_GetFile", "Choose a file" ,
                       title = "Please select a file:", multiple = TRUE,
                       buttonType = "default", class = NULL),
    
      textOutput("filename"), 
      textOutput("txt_file")
    )
    
    rvs <- reactiveValues() #renamed to rvs
    server <- function(input,output,session){
    
      volumes = getVolumes()
      observe({  
        shinyFileChoose(input, "Btn_GetFile", roots = volumes, session = session)
    
        req(input$Btn_GetFile)
    
        file_selected <- parseFilePaths(volumes, input$Btn_GetFile)
        output$txt_file <- renderText(as.character(file_selected$datapath))
        output$filename <- renderText(as.character(file_selected$name))
        rvs$filepath <- as.character(file_selected$datapath)
        print(rvs$filepath) #Added this print to test
    
      })
    }
    observe({
      req(rvs$filepath)
      system(glue("echo {rvs$filepath}"))
    })
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 如何从 bash 块中调用 rvs$filepath?我不理解反应式上下文的概念。 Sys.setenv(file = rvs$filepath) 不适用于{bash} echo $file
    • 为什么要将它放在 bash 块中?你能详细说明你想要达到的目标吗?
    • 我正在尝试调用外部程序来处理交互式文件输入。所以,如果我可以使用 shiny 来获取输入文件并将其传递给我可以调用程序的 bash 块,我会很高兴。 {bash} binary.bin rvs$fileinput -a parameter1 -b parameter2 &gt; output.file 从 R 中调用外部程序会更容易吗,比如 python 的 os.system()?
    • 已编辑我的答案。你可以试试system() 函数。让我知道它是否有效。
    • 很高兴它有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 2020-08-22
    • 1970-01-01
    • 2018-01-18
    • 2014-09-30
    • 1970-01-01
    相关资源
    最近更新 更多