对于我正在构建的Shiny应用程序的一部分,我需要让用户选择一个目录.目录路径存储在一个反应变量中.用户可以从文件窗口中选择目录,也可以手动输入路径textInput
.我已经想出如何做到这一点,但我不明白为什么我的解决方案有效!工作应用程序的最小示例:
library(shiny)
ui <- fluidPage(
actionButton("button1", "First Button"),
textInput("inText", "Input Text"),
actionButton("button2", "Second Button"),
textOutput("outText"),
textOutput("outFiles")
)
server <- function(input, output) {
values <- reactiveValues(inDir = NULL)
observeEvent(input$button1, {values$inDir <- tcltk::tk_choose.dir()})
observeEvent(input$button2, {values$inDir <- input$inText})
inPath <- eventReactive(values$inDir, {values$inDir})
output$outText <- renderText(inPath())
fileList <- reactive(list.files(path=inPath()))
output$outFiles <- renderPrint(fileList())
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
我尝试的第一件事就是使用eventReactive
并将两个输入源分配给反应变量:
server <- function(input, output) {
inPath <- eventReactive(input$button1, {tcltk::tk_choose.dir()})
inPath <- eventReactive(input$button2, {input$inText})
output$outText <- renderText(inPath())
fileList <- reactive(list.files(path=inPath()))
output$outFiles <- renderPrint(fileList())
}
Run Code Online (Sandbox Code Playgroud)
据我所知,这种效果只有一个按钮可以做任何事情.我真正不明白的是为什么这不起作用.我认为会发生的第一个按钮会创建 …
我在编写的脚本中遇到错误,只有在我dplyr
运行时才会发生.当我找到一个dplyr
我想要使用的函数时,我第一次遇到它,之后我安装并运行了包.以下是我的错误示例:
首先,我从excel中读取一个表,其中包含我将用作索引的列值:
library(readxl)
examplelist <- read_excel("example.xlsx")
Run Code Online (Sandbox Code Playgroud)
该文件的内容是:
1 2 3 4
1 1 4 1
2 3 2 1
4 4 1 4
Run Code Online (Sandbox Code Playgroud)
然后我构建了一个数据框:
testdf = data.frame(1:12, 13:24, 25:36, 37:48)
Run Code Online (Sandbox Code Playgroud)
然后我有一个循环调用一个使用examplelist
as作为索引值的函数.
testfun <- function(df, a, b, c, d){
value1 <- df[[a]]
value2 <- df[[b]]
value3 <- df[[c]]
value4 <- df[[d]]
}
for (i in 1:nrow(examplelist)){
testfun(testdf, examplelist[i, 1], examplelist[i, 2],
examplelist[i, 3], examplelist[i, 4])
}
Run Code Online (Sandbox Code Playgroud)
当我没有运行这个脚本时dplyr
,一切都很好,但是dplyr
它给了我错误:
Error in .subset2(x, i, …
Run Code Online (Sandbox Code Playgroud) 我想使用UTF-8编码获取字符串中的字节数而不显式创建字节数组(因为我不需要使用数组,只需要字节数).这可能吗?我的问题几乎就是这个,但用C#而不是Java.
谢谢!
将向量写入文件的最快方法是什么?我有一个大约200万行的字符向量,它有相当大的值(200个字符).我现在正在做
write(myVector, "myFile.txt")
Run Code Online (Sandbox Code Playgroud)
但这非常缓慢.我一直在寻找解决方案,但快速写入功能(例如fwrite
)仅将数据帧/矩阵作为输入.谢谢!
我遇到了一个问题,即使我禁用指数表示法,也会以指数表示法fwrite
打印数字.一个例子:
library(data.table)
options(scipen = 999)
testint = c(500000)
Run Code Online (Sandbox Code Playgroud)
在我打印之前,r
表现和不以指数表示法打印:
print(testint)
[1] 500000
print(list(testint)
[[1]]
[1] 500000
Run Code Online (Sandbox Code Playgroud)
但当我这样做时:
fwrite(list(testint), "output")
Run Code Online (Sandbox Code Playgroud)
该文件的内容是5e + 05.我怀疑这个问题可能特别适用fwrite
,就像我一样:
write(testint, "output1")
Run Code Online (Sandbox Code Playgroud)
输出文件的内容为500000.
有没有办法阻止fwrite
这样做?我可以切换到使用write
,但它们之间存在巨大的速度差异,而且我正在编写大量数据,因此如果可能的话,我会想要避免显着的性能影响.谢谢!
编辑:如果有人有兴趣,有一个现有的开放GitHub的问题在这里,我发现后,我问的问题!
我有一个 Shiny 应用程序,我想在其中使用操作按钮添加 UI 元素,然后让插入的 ui 成为动态的。
这是我当前的 ui 文件:
library(shiny)
shinyUI(fluidPage(
div(id="placeholder"),
actionButton("addLine", "Add Line")
))
Run Code Online (Sandbox Code Playgroud)
和服务器文件:
library(shiny)
shinyServer(function(input, output) {
observeEvent(input$addLine, {
num <- input$addLine
id <- paste0("ind", num)
insertUI(
selector="#placeholder",
where="beforeBegin",
ui={
fluidRow(column(3, selectInput(paste0("selected", id), label=NULL, choices=c("choice1", "choice2"))))
})
})
})
Run Code Online (Sandbox Code Playgroud)
如果choice1
在特定的 ui 元素中选择了 ,我想textInput
在行中添加一个。如果choice2
在 ui 元素中选择了 ,我想添加一个numericInput
.
虽然我通常了解如何创建响应用户输入而改变的反应值,但我不知道在这里做什么,因为我不知道如何观察尚未创建的元素并且我不知道名称的。任何帮助将不胜感激!
有没有办法防止as.character
使用指数表示法?例如,我有
num <- c(9999999, 10000000)
char <- as.character(num)
char
[1] "9999999" "1e+07"
Run Code Online (Sandbox Code Playgroud)
但相反,我想char
成为"9999999" "10000000"
. 谢谢!