有没有办法将R中定义的变量传递给RODBC包中的sqlQuery函数?
具体来说,我需要将这样的变量传递给标量/表值函数,存储过程和/或SELECT语句的WHERE子句.
例如,让:
x <- 1 ## user-defined
Run Code Online (Sandbox Code Playgroud)
然后,
example <- sqlQuery(myDB,"SELECT * FROM dbo.my_table_fn (x)")
Run Code Online (Sandbox Code Playgroud)
要么...
example2 <- sqlQuery(myDB,"SELECT * FROM dbo.some_random_table AS foo WHERE foo.ID = x")
Run Code Online (Sandbox Code Playgroud)
要么...
example3 <- sqlQuery(myDB,"EXEC dbo.my_stored_proc (x)")
Run Code Online (Sandbox Code Playgroud)
显然,这些都不起作用,但我认为有一些能够实现这种功能的东西.
超级快速的问题......
如何获取某个特定函数(用户定义的)参数并将其转换为字符串?
如果是一个简单的例子,
foo <- function(x) { ... }
Run Code Online (Sandbox Code Playgroud)
我想简单地返回x的对象名称.所以,
foo(testing123)
Run Code Online (Sandbox Code Playgroud)
返回"testing123"(并且testing123可能只是一些随机数字向量)
如果之前已经问过这个问题,我会道歉 - 搜索,但找不到它!谢谢!!
反正我是否可以遍历一些对象并为每个对象应用一个函数?
当我键入ls()或时objects(),它返回一个对象名称列表.我想迭代这个列表,识别那些data.frame,然后针对每个对象运行一个函数.
如何从函数ls或objects通过函数传递条目?
如果这看起来像一个愚蠢的问题,请道歉......
所以我有一个非常大的术语文档矩阵:
> class(ph.DTM)
[1] "TermDocumentMatrix" "simple_triplet_matrix"
> ph.DTM
A term-document matrix (109996 terms, 262811 documents)
Non-/sparse entries: 3705693/28904453063
Sparsity : 100%
Maximal term length: 191
Weighting : term frequency (tf)
Run Code Online (Sandbox Code Playgroud)
如何获得每个术语的rowSum(频率)?我试过了:
> apply(ph.DTM, 1, sum)
Error in vector(typeof(x$v), nr * nc) : vector size cannot be NA
In addition: Warning message:
In nr * nc : NAs produced by integer overflow
Run Code Online (Sandbox Code Playgroud)
显然,我知道removeSparseTerms:
ph.DTM2 <- removeSparseTerms(ph.DTM, 0.99999)
Run Code Online (Sandbox Code Playgroud)
这减少了一点:
> ph.DTM2
A term-document matrix (28842 terms, 262811 documents)
Non-/sparse entries: …Run Code Online (Sandbox Code Playgroud) 在我正在研究的事情上,我遇到了一些打嗝.假设我有以下简单示例.让...
v <- c(606:608) ## Some vector of integers
Run Code Online (Sandbox Code Playgroud)
我也有一个单独的脚本(让我们只是调用它foo.R)有类似的东西(使用RODBC包):
um <- sqlQuery(artemis,paste("select * from port.tdtf_VaR_Unmatched (",LatestModelRun,")",sep=""))
Run Code Online (Sandbox Code Playgroud)
现在假设我想运行以下循环函数:
test <- function() {
for (i in 1:length(v)) {
LatestModelRun <- v[i]
source("C:/R/foo.r")
print(unmatched)} }
test() ## Run it
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我收到以下错误:
Error in paste("\n\tselect * from port.tdtf_VaR_Unmatched (", LatestModelRun, :
object 'LatestModelRun' not found
所以,不知何故,它不是在函数中LatestModelRun定义的变量中读取test.
这是traceback():
7: paste("\n\tselect * from port.tdtf_VaR_Unmatched (", LatestModelRun,
")\n\twhere [PortfolioProduct] not in ('REC - Generic','REC - …Run Code Online (Sandbox Code Playgroud) 假设我有以下内容:
foo <- data.frame(Company = c("company1", "foo", "test", "food"), Metric = rnorm(4, 10))
> foo
Company Metric
1 company1 10.539970
2 foo 9.487823
3 test 9.663994
4 food 9.499327
Run Code Online (Sandbox Code Playgroud)
为什么dplyr::filter返回0结果?(而不是第2和第4行)我正在尝试在特定的输入字符串上使用SQL等效的通配符过滤器%like%.
我究竟做错了什么?
我有以下数据框:
> DF
Year Metric MWh
2003 Demand 498343
2004 Demand 1250904
2005 Demand 1665176
2006 Demand 2317643
2007 Demand 2455311
2008 Demand 3557987
2009 Demand 4268125
2010 Demand 5403704
2011 Demand 6596158
2012 Demand 7814387
2013 Demand 9008863
2014 Demand 10291085
2015 Demand 11796549
2003 Actual 159677
2004 Actual 192748
2005 Actual 248844
2006 Actual 372661
2007 Actual 705656
2008 Actual 838721
2009 Actual 1188242
2010 Actual 1708979
2011 Actual 0
2012 Actual 0
2013 Actual 0
2014 Actual …
我正在使用该RODBC包来查询数据库中的文本列.该数据库基于Microsoft SQL Server 2008 R2构建.SQL中列的数据类型是nvarchar(max).
但是,当我跑:
# Set up ODBC connection to CCWEB5 production server
# Note: default database is set to "CCSalary"
ccweb5.prod <- odbcConnect("ccweb5")
# Read in some job ad text
job.text <- sqlQuery(ccweb5.prod,"
SELECT TOP 100
ja.JobTitle,
ja.JobText as 'JobText',
LEN(ja.JobText) as 'JobTextLength'
FROM JobStore.dbo.JobAd as ja (NOLOCK)
")
Run Code Online (Sandbox Code Playgroud)
在SQL中,我期待(对于顶行):
JobTitle JobText JobTextLength
IT Field Service Technician <text goes here...> 2742
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时: nchar(as.character(job.text[1,2]))
它返回:255.
所以我的问题是,导致这种截断的原因是什么,我该如何避免呢?谢谢!!
首先,这是一个最小的可重现示例:
假设我有以下flexdashboard应用程序example.Rmd:
---
title: "Test App"
output:
flexdashboard::flex_dashboard:
theme: ["lumen"]
orientation: ["columns"]
runtime: shiny
---
```{r setup, include=FALSE}
source('modules.R')
```
Test
==================
Inputs {.sidebar data-width=250}
------------------
```{r}
## Modules:
globalInputs <- callModule(setup, "inputs") # Note: you only need to call this one time!
callModule(chart, "modalChart", globalInputs)
## UI:
sidebar("inputs")
```
Column
-------------------------------------
### ModalTest
```{r}
chartUI2("modalChart")
```
Run Code Online (Sandbox Code Playgroud)
这是我的module.R文件:
sidebar <- function(id) {
ns <- NS(id)
tagList(
helpText("Press the button below to pull up a …Run Code Online (Sandbox Code Playgroud) 作为一个最小可行的例子,我模块化了这里的基本例子:https : //rmarkdown.rstudio.com/flexdashboard/shiny.html#simple_example
.Rmd在 RStudio 中运行应该可以解决问题):---
title: "stackoverflow example"
output: flexdashboard::flex_dashboard
runtime: shiny
---
```{r global, include=FALSE}
library(shiny)
library(flexdashboard) # install.packages("flexdashboard")
# load data in 'global' chunk so it can be shared by all users of the dashboard
library(datasets)
data(faithful)
# UI modules
sidebarCharts <- function(id) {
ns <- NS(id)
tagList(
p(),
actionButton(ns("settings"), "Settings", icon = icon("cogs"), width = '100%', class = "btn btn-info"),p(),
actionButton(ns("refreshMainChart") ,"Refresh", icon("refresh"), width = '100%', class = "btn btn-primary"),p() …Run Code Online (Sandbox Code Playgroud)